﻿@using NGroup.VTL.Models;
@using NGroup.VTL.WebCore;
@using NGroup.VTL.Core.Const;
@model List<GenericTypeItem>
@{
    ViewBag.Title = "Quản lý loại sản phẩm";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
@section Head{
    <link rel="Stylesheet" href="@Url.Content("~/Content/adminpages/css/style.min.css")" type="text/css"  />
    <script type="text/javascript" src="@Url.Content("~/Content/adminpages/scripts/jstree.min.js")"></script>
}
<div class="clear">
</div>
<div class="content-box">
    <div class="content-box-header">
        <h3>
            Quản lý loại sản phẩm</h3>
        <div class="filterGroup">
            <a href="#questionProductType" title="Hướng dẫn" rel="modal">
                <img src="@Url.Content("~/Content/adminpages/images/icons/question.png")" alt="Hướng dẫn" /></a>
        </div>
        <div class="filterGroup" style="padding-top: 9px;">
            @*<input class="button" onclick="type_create();" type="button" value="Thêm" />
            <input class="button" onclick="type_delete();" type="button" value="Xóa" />*@ <a
                href="#formPostProductType" title="Thêm" rel="modal" class="button">Thêm</a>
            <a href="#formPostProductTypeEdit" title="Sửa" rel="modal" class="button">Sửa</a>
            @using (Html.BeginForm("xoa_loai_san_pham", "vuarao", FormMethod.Post, new { @id = "formPostProductTypeDelete", @class = "formPostNew", @style = "display: inline;" }))
            {
                <input type="hidden" id="typeIDDel" name="typeID" value="" />
                <input type="submit" class="button" title="Xóa" onmousedown="type_delete();" style="padding: 4px 15px;"
                    value="Xóa" id="btnDelType" />
            }
        </div>
        <div class="clear">
        </div>
    </div>
    <div class="content-box-content">
        <div style="width: 30%">
            <div id="jstree_productType" class="jstree_productType">
                @if (Model != null && Model.Count > 0)
                {
                    <ul>
                        @foreach (var item in Model)
                        {
                            if (item.SupTypeID == null || (item.SupTypeID != null && item.SupTypeID <= 0))
                            {
                            <li><a href="@item.UniqueString" rev="@item.CanEdit" rel="@item.TypeID" title="@item.Description|---|@item.Description_En|---|@item.MetaDescription|---|@item.MetaDescription_En|---|@item.MetaKeyword|---|@item.MetaKeyword_En">@item.Description</a>
                                @foreach (var item1 in Model)
                                {
                                    <ul>
                                        @if (item1.SupTypeID != null && item1.SupTypeID == item.TypeID)
                                        { 
                                            <li><a href="@item1.UniqueString" rev="@item1.CanEdit" rel="@item1.TypeID" title="@item1.Description|---|@item1.Description_En|---|@item1.MetaDescription|---|@item1.MetaDescription_En|---|@item1.MetaKeyword|---|@item1.MetaKeyword_En">@item1.Description</a></li>
                                        }
                                    </ul>
                                }
                            </li>
                            }
                        }
                    </ul>
                }
            </div>
        </div>
        <div id="productTypeDetail" class="productTypeDetail">
            @using (Html.BeginForm("quan_ly_loai_san_pham", "vuarao", FormMethod.Post, new { @id = "formPostProductType", @class = "formPostNew", @onsubmit = "return clickFix(this)" }))
            {
                <p>
                    <label>
                        Mô tả (Vi)</label>
                    <input class="text-input medium-input" placeholder="Mô tả (Vi)" name="typeName" type="text"
                        id="type_name" />
                    <br />
                </p>
                <p>
                    <label>
                        Mô tả (En)</label>
                    <input class="text-input medium-input" placeholder="Mô tả (En)" name="typeNameEn"
                        type="text" id="type_name_en" />
                    <br />
                </p>
                <p>
                    <label>
                        Liên kết</label>
                    <input class="text-input medium-input" placeholder="Liên kết" name="typeLink" type="text"
                        id="type_link" />
                    <br />
                </p>
                <p>
                    <label>
                        MetaKeywords (Vi)</label>
                    <input class="text-input medium-input" placeholder="Metakeywords (Vi)" name="metaKeyword"
                        type="text" id="type_metakeyword" />
                    <br />
                </p>
                <p>
                    <label>
                        MetaKeywords (En)</label>
                    <input class="text-input medium-input" placeholder="MetaKeywords (En)" name="metaKeywordEn"
                        type="text" id="type_metakeyword_en" />
                    <br />
                </p>
                <p>
                    <label>
                        MetaDescriptions (Vi)</label>
                    <input class="text-input medium-input" placeholder="MetaDescriptions (Vi)" name="metaDes"
                        type="text" id="type_metaDes" />
                    <br />
                </p>
                <p>
                    <label>
                        MetaDescriptions (En)</label>
                    <input class="text-input medium-input" placeholder="MetaDescriptions (En)" name="metaDesEn"
                        type="text" id="type_metaDes_en" />
                    <br />
                </p>
                <p>
                    <input type="hidden" id="typeID" name="typeID" value="" />
                    <input type="hidden" id="CanEdit" name="CanEdit" value="true" />
                    <input type="hidden" id="parentID" name="parentID" value="" />
                    <label>
                    </label>
                    <input type="submit" id="btnAddNew" class="button" value="Lưu" />
                </p>
            }
            @using (Html.BeginForm("quan_ly_loai_san_pham", "vuarao", FormMethod.Post, new { @id = "formPostProductTypeEdit", @class = "formPostNew", @onsubmit = "return clickFix(this)" }))
            {
                <p>
                    <label>
                        Mô tả (Vi)</label>
                    <input class="text-input medium-input" placeholder="Mô tả (Vi)" name="typeName" type="text"
                        id="type_name_edit" />
                    <br />
                </p>
                <p>
                    <label>
                        Mô tả (En)</label>
                    <input class="text-input medium-input" placeholder="Mô tả" name="typeNameEn" type="text"
                        id="type_name_edit_en" />
                    <br />
                </p>
                <p>
                    <label>
                        MetaKeywords (Vi)</label>
                    <input class="text-input medium-input" placeholder="Metakeywords (Vi)" name="metaKeyword"
                        type="text" id="type_metakeyword_edit" />
                    <br />
                </p>
                <p>
                    <label>
                        MetaKeywords (En)</label>
                    <input class="text-input medium-input" placeholder="MetaKeywords (En)" name="metaKeywordEn"
                        type="text" id="type_metakeyword_edit_en" />
                    <br />
                </p>
                <p>
                    <label>
                        MetaDescriptions (Vi)</label>
                    <input class="text-input medium-input" placeholder="MetaDescriptions (Vi)" name="metaDes"
                        type="text" id="type_metades_edit" />
                    <br />
                </p>
                <p>
                    <label>
                        MetaDescriptions (En)</label>
                    <input class="text-input medium-input" placeholder="MetaDescriptions (En)" name="metaDesEn"
                        type="text" id="type_metades_edit_en" />
                    <br />
                </p>
                <p>
                    <input type="hidden" id="typeID_edit" name="typeID" value="" />
                    <input type="hidden" id="CanEdit_edit" name="CanEdit" value="true" />
                    <input type="hidden" id="parentID_edit" name="parentID" value="" />
                    <label>
                    </label>
                    <input type="submit" id="btnEdit" class="button" value="Lưu" />
                </p>
            }
        </div>
        <div class="clear">
        </div>
    </div>
</div>
<div class="clear">
</div>
<input type="hidden" id="chkSaveFlag" />
<script type="text/javascript">
    $(function () {
        $('#jstree_productType').jstree({
            "core": {
                "multiple": false,
                "check_callback": true
            }
        });

        //$('#jstree_productType').jstree(true).open_all();

        $('#jstree_productType').on("changed.jstree", function (e, data) {
            if (!data.selected[0] == false) {
                var obj = $('#jstree_productType').jstree(true).get_node(data.selected.toString());
                var obj1 = $('#jstree_productType').jstree(true).get_node(obj.parent);

                //$("#type_link_edit").val(obj.a_attr.href);

                if (obj.a_attr.rel == 1 || obj.a_attr.rel == 2) {
                    $("#formPostProductTypeDelete #btnDelType").hide();
                }
                else {
                    $("#formPostProductTypeDelete #btnDelType").show();
                }

                $("#typeID_edit").val(obj.a_attr.rel);
                $("#typeIDDel").val(obj.a_attr.rel);
                $("#CanEdit_edit").val(obj.a_attr.rev);
                $("#parentID").val(obj.a_attr.rel);

                var arrTitle = SplitTitle(obj.a_attr.title);

                $("#type_name_edit").val(arrTitle[0]);
                $("#type_name_edit_en").val(arrTitle[1]);
                $("#type_metades_edit").val(arrTitle[2]);
                $("#type_metades_edit_en").val(arrTitle[3]);
                $("#type_metakeyword_edit").val(arrTitle[4]);
                $("#type_metakeyword_edit_en").val(arrTitle[5]);

                $("#parentID_edit").val(obj.parent != "#" ? obj1.a_attr.rel : obj.parent);
            }
        });
    });
    function type_delete() {
        var ref = $('#jstree_productType').jstree(true), sel = ref.get_selected();

        if (!sel.length) {
            return false;
        }

        var isConfirmed = confirm('Bạn thực sự muốn xóa?');

        if (isConfirmed) {
            $("#formPostProductTypeDelete").submit();
        }

        event.preventDefault();
    };
    var check = false;
    function clickFix(obj) {
        if ((obj.id == "formPostProductType" && (
                                        $.trim($("#facebox #type_name").val()) == "" ||
                                        $.trim($("#facebox #type_name_en").val()) == "" ||
                                        $.trim($("#facebox #type_link").val()) == ""
                                        )) ||
               (obj.id == "formPostProductTypeEdit" && (
                                        $.trim($("#facebox #type_name_edit").val()) == "" ||
                                        $.trim($("#facebox #type_name_edit_en").val()) == ""
                                        ))
            ) {
            alert("Hãy điền đầy đủ thông tin");
            return false;
        }
        if (obj.id == "formPostProductType") {
            var ref = $('#jstree_productType').jstree(true), sel = ref.get_selected();
            if ($("#" + sel[0]).parents("ul").length == 2) {
                alert("Chỉ cho phép lưu 2 cấp!");
                return false;
            }
        }
        if (!check) {
            var uniqueString = null;
            var supTypeID = $(obj).find("input[name=parentID]").val();
            var typeID = $(obj).find("input[name=typeID]").val();

            if (obj.id == "formPostProductType") {
                uniqueString = $.trim($("#facebox #type_link").val());

                var request = $.ajax({ type: "POST",
                    url: "/vuarao/CheckUniqueStringGT",
                    data: { supTypeID: supTypeID, typeID: typeID, uniqueString: uniqueString }
                });

                request.done(function (msg) {
                    if (msg.toLowerCase() == "true") {
                        // Add new type, BAD if not existing
                        alert("Liên kết đã tồn tại ");
                        return false;
                    } else {
                        check = true;
                        $(obj).submit();
                    }
                });
                request.fail(function (jqXHR, textStatus) {
                    return false;
                });
            }
            else {
                check = true;
                $(obj).submit();
            }

            return false;
        }
    };

    function SplitTitle(titleString) {
        if (!titleString == false && titleString.indexOf('|---|') > -1) {
            return titleString.split('|---|');
        }
    }
</script>
<div id="questionProductType" style="display: none; width: 550px; font-size: 140%;
    line-height: 21px;">
    <h3>
        Hướng dẫn</h3>
    <p>
        <ul>
            <li style="padding: 0px 10px 10px;">- Liên kết không được bỏ trống, và có độ dài từ
                5 đến 500 ký tự, không có ký tự đặc biệt.<br />
                ví dụ : "the-thanh-vien-cho-khach-hang-than-thiet"</li>
            <li style="padding: 0px 10px 10px;">- Mô tả không được bỏ trống, và có độ dài từ 5 đến
                500 ký tự.<br />
                ví dụ : "Thẻ thành viên"</li>
            <li style="padding: 0px 10px 10px;">- Chỉ cho phép thêm 2 cấp.</li>
            <li style="padding: 0px 10px 10px;">- Khi không có loại nào được chọn,Thêm mới sẽ lưu
                vào cấp 1.</li>
        </ul>
    </p>
</div>
@section Lazy{
    <script type="text/javascript">
        $(".current").removeClass("current");
        $("#qllsp").addClass("current");

        $(document).ready(function () {
            $("#jstree_productType a.jstree-anchor").click(function () {
                var clickedRelID = parseInt($(this).attr("rel"));

                if (clickedRelID == 1 || clickedRelID == 2) {
                    $("#formPostProductTypeDelete #btnDelType").hide();
                }
                else {
                    $("#formPostProductTypeDelete #btnDelType").show();
                }
            });
        });

    </script>
}
